//
//  UIUtil.m
//  UIControls
//
//  Created by 21CN Vgo on 11-11-18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UIUtil.h"


@implementation UIUtil

+ (UIImage *)imageWithName:(NSString *)imageName
{
	NSString * pathExtension = [imageName pathExtension];
	NSString * pathName = [imageName stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@",pathExtension] withString:@""];
	
	NSString* imagePath = [[NSBundle mainBundle] pathForResource:pathName ofType:pathExtension];
	UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
	
	return image;
}

+ (UIImage *)tileImage:(UIImage *)image withSize:(CGSize)size
{
	CGFloat scale = [UIScreen mainScreen].scale;
	if (scale > 1.0)
	{
		UIGraphicsBeginImageContextWithOptions(size, NO, scale);
	}
	else
	{
		UIGraphicsBeginImageContext(size);
	}
	
	CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	//  Save the context's state so that the translate and scale can be undone with a call
	//  to CGContextRestoreGState.
	CGContextSaveGState(context);
	
	CGContextClipToRect(context, CGRectMake(0.0, 0.0, size.width, size.height));
	
	CGContextScaleCTM(context, 1, -1);
	
	CGContextDrawTiledImage(context, imageRect, [image CGImage]);
	
	UIImage * tileImage = UIGraphicsGetImageFromCurrentImageContext();
	
	// Restore the context's state. This removes the translation and scaling
	// but leaves the path, since the path is not part of the graphics state.
	CGContextRestoreGState(context);
	
	UIGraphicsEndImageContext();
	
	return tileImage;
}

+ (UIImage*)getGrayImage:(UIImage*)sourceImage
{
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL) {
        return nil;
    }
    CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *grayImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGContextRelease(context);
    return grayImage;
}

+ (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size
{
	CGFloat scale = [UIScreen mainScreen].scale;
	if (scale > 1.0)
	{
		UIGraphicsBeginImageContextWithOptions(size, NO, scale);
	}
	else
	{
		UIGraphicsBeginImageContext(size);
	}
	
	CGRect imageRect = CGRectMake(0, 0, size.width, size.height);
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	//  Save the context's state so that the translate and scale can be undone with a call
	//  to CGContextRestoreGState.
	CGContextSaveGState(context);
	
	CGContextSetFillColorWithColor(context, color.CGColor);
	CGContextFillRect(context, imageRect);
	
	UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
	
	// Restore the context's state. This removes the translation and scaling
	// but leaves the path, since the path is not part of the graphics state.
	CGContextRestoreGState(context);
	
	UIGraphicsEndImageContext();
	
	return image;
}

+ (UIImage *)progressImageWitchColor:(UIColor *)progClr backgroudColor:(UIColor *)bgClr size:(CGSize)size progress:(CGFloat)progress
{
	CGFloat scale = [UIScreen mainScreen].scale;
	if (scale > 1.0)
	{
		UIGraphicsBeginImageContextWithOptions(size, NO, scale);
	}
	else
	{
		UIGraphicsBeginImageContext(size);
	}
	
	CGRect progressRect = CGRectMake(0, 0, size.width * progress, size.height);
	CGRect imageRect = CGRectMake(progressRect.size.width, 0, size.width - progressRect.size.width, size.height);
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	//  Save the context's state so that the translate and scale can be undone with a call
	//  to CGContextRestoreGState.
	CGContextSaveGState(context);
	
//	CGContextClipToRect(context, CGRectMake(0.0, 0.0, size.width, size.height));
//	
//	CGContextScaleCTM(context, 1, -1);
//	
//	CGContextDrawTiledImage(context, imageRect, [image CGImage]);
	
	CGContextSetFillColorWithColor(context, bgClr.CGColor);
	CGContextFillRect(context, imageRect);
	
	CGContextSetFillColorWithColor(context, progClr.CGColor);
	CGContextFillRect(context, progressRect);
	
	
	UIImage * progressImage = UIGraphicsGetImageFromCurrentImageContext();
	
	// Restore the context's state. This removes the translation and scaling
	// but leaves the path, since the path is not part of the graphics state.
	CGContextRestoreGState(context);
	
	UIGraphicsEndImageContext();
	
	return progressImage;
}

@end
